import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.codehaus.groovy.grails.plugins.springsecurity.ui.RegistrationCode

//package br.com.jambu.cge.security


class RegisterController extends grails.plugins.springsecurity.ui.RegisterController {

	def forgotPassword = {
		
				if (!request.post) {
					// show the form
					return
				}
		
				String username = params.email
				
				if (!username || !((username ==~ /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/)) ) {
					flash.error = message(code: 'spring.security.ui.forgotPassword.username.missing')
					redirect action: 'forgotPassword'
					return
				}
		
				String usernameFieldName = SpringSecurityUtils.securityConfig.userLookup.emailPropertyName
				def user = lookupUserClass().findWhere((usernameFieldName): username)
				if (!user) {
					flash.error = message(code: 'spring.security.ui.forgotPassword.user.notFound')
					redirect action: 'forgotPassword'
					return
				}
		
				def registrationCode = new RegistrationCode(username: user.username)
				registrationCode.save(flush: true)
		
				String url = generateLink('resetPassword', [t: registrationCode.token])
		
				def conf = SpringSecurityUtils.securityConfig
				def body = conf.ui.forgotPassword.emailBody
				if (body.contains('$')) {
					body = evaluate(body, [user: user, url: url])
				}
				mailService.sendMail {
					to user.email
					from conf.ui.forgotPassword.emailFrom
					subject conf.ui.forgotPassword.emailSubject
					html body.toString()
				}
		
				[emailSent: true]
			}
	
	
}